package com.zh.common.service.impl;

import com.zh.common.dao.UserDao;
import com.zh.common.dao.UserRoleDao;
import com.zh.common.domain.UserDO;
import com.zh.common.domain.UserRoleDO;
import com.zh.common.utils.MD5Utils;
import com.zh.common.utils.StringUtil;
import com.zh.common.vo.CompanyUserVO;
import com.zh.common.vo.FamilyUserVO;
import com.zh.common.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;

	@Autowired
	private UserRoleDao userRoleDao;
	
	@Override
	public UserDO get(String userId){
		List<String> roleIds = userRoleDao.listRoleId(userId);
		UserDO user = userDao.get(userId);
		user.setRoleIds(roleIds);
		return user;
	}

	@Override
	public List<UserDO> list(Map<String, Object> map){
		return userDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return userDao.count(map);
	}
	
	@Override
	@Transactional
	public int
	save(UserDO user){
		user.setUserId(StringUtil.generalKeyword());
//		密码加密
		user.setPassword(MD5Utils.encrypt(user.getPassword()));
//		保存用户
		int count = userDao.save(user);
//		获取用户id
		String userId = user.getUserId();
		if(user.getRoleIds()!=null){
//		获取角色
			List<String> roles = user.getRoleIds();
//		删除用户角色
			userRoleDao.removeByUserId(userId);
			List<UserRoleDO> list = new ArrayList<>();
			for (String roleId : roles) {
				UserRoleDO ur = new UserRoleDO();
				ur.setId(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date())+(int)((Math.random()*9+1)*1000));
				ur.setUserId(userId);
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if (list.size() > 0) {
				userRoleDao.batchSave(list);
			}
		}
		return count;
	}
	
	@Override
	@Transactional
	public int update(UserDO user){
		user.setPassword(MD5Utils.encrypt(user.getPassword()));
		String userId = user.getUserId();
		List<String> roles = user.getRoleIds();
		if(roles!=null){
			userRoleDao.removeByUserId(userId);
			List<UserRoleDO> list = new ArrayList<>();
			for (String roleId : roles) {
				UserRoleDO ur = new UserRoleDO();
				ur.setId(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date())+(int)((Math.random()*9+1)*1000));
				ur.setUserId(userId);
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if (list.size() > 0) {
				userRoleDao.batchSave(list);
			}
		}
		return userDao.update(user);
	}


	@Override
	@Transactional
	public int remove(String userId){
		userRoleDao.removeByUserId(userId);
		return userDao.remove(userId);
	}
	
	@Override
	@Transactional
	public int batchRemove(String[] userIds){
		int count = userDao.batchRemove(userIds);
		userRoleDao.batchRemoveByUserId(userIds);
		return count;
	}

	@Override
	public UserDO getByUserName(String userName, int userType) {
		Map map=new HashMap<>();
		map.put("username",userName);
		map.put("userType",userType);
		return userDao.getByUserName(map);
	}

	@Override
	public CompanyUserVO getCompanyUser(String userName, int userType) {
		Map map=new HashMap<>();
		map.put("username",userName);
		map.put("userType",userType);
		return userDao.getCompanyUser(map);
	}

	@Override
	public FamilyUserVO getFamilyUser(String userName, int userType) {
		Map map=new HashMap<>();
		map.put("username",userName);
		map.put("userType",userType);
		return userDao.getFamilyUser(map);
	}

}
